package com.mytest.baodianofsunflower;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.mytest.util.BaseActivity;
import com.mytest.util.GetNetInfos;
import com.mytest.util.Globals;
import com.mytest.util.QuestionDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QuestionActivity extends BaseActivity {
    private ListView list;
    private List<Map<String, Object>> values = new ArrayList<>();
    private SimpleAdapter adapter;
    private Button refreshBtn;
    //分页使用
    private int pagenum = 1;
    private int pagesize = 20;
    private int recordCount;
    private int pageCount;
    private String keyword;

    //list的onScroll事件使用
    private int first;
    private int visible;
    private int total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        super.init("全部问题", 1);

        list = (ListView) findViewById(R.id.list);
        keyword = getIntent().getStringExtra("keyword");
        if(keyword==null) {
            keyword = "";
        }
        //分页查询数据
        values = QuestionDao.loadDataPage(pagenum, pagesize, keyword);
        Log.i("hahah","---$$$$$---"+values.size());
        //查询总记录数
        recordCount = QuestionDao.getCount(keyword);
        //总页数
        pageCount = (recordCount - 1) / pagesize + 1;

        //加入尾部footerView
        final TextView footerView = new TextView(this);
        footerView.setText("正在加载数据库，请稍候····");
        footerView.setTextSize(20);
        footerView.setTextColor(Color.BLACK);
        list.addFooterView(footerView);
        adapter = new SimpleAdapter(this, values, R.layout.question_list, new String[]{"question"}, new int[]{R.id.list_question});
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = values.get(position);
                LinearLayout linear = (LinearLayout) view;
                //linear.getChildAt(1).setVisibility(View.VISIBLE);
                if (linear.getChildCount() == 1) {
                    //如果当前只有一行，加入答案
                    TextView answerView = new TextView(QuestionActivity.this);
                    answerView.setText(map.get("answer").toString());
                    answerView.setTextSize(14);
                    answerView.setTextColor(Color.RED);
                    linear.addView(answerView);
                } else {
                    //否则删除答案行
                    linear.removeViewAt(1);
                }

            }
        });
        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE && first + visible == total && total != 0) {
                    if (pagenum < pageCount) {
                        pagenum++;
                        //将分页数据加入到原有的数据上
                        values.addAll(QuestionDao.loadDataPage(pagenum, pagesize, keyword));
                        //通知
                        adapter.notifyDataSetChanged();
                    } else {
                        //如果到底部，去除footerView
                        if (list.getFooterViewsCount() != 0) {
                            list.removeFooterView(footerView);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;

            }
        });
        refreshBtn = (Button) findViewById(R.id.refresh);
        refreshBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetNetInfos.getNewsInfo();
                Toast.makeText(QuestionActivity.this,"更新数据中····",Toast.LENGTH_LONG).show();
                values.clear();
                values.addAll(QuestionDao.loadDataPage(pagenum, pagesize, keyword));
                adapter.notifyDataSetChanged();
            }
        });
    }
}

